<?php
session_start();
//require_once("../config/include.inc.php");
require_once("../config/include_tokyo.inc.php");

//访问权限验证
$userRole = $_SESSION['user_role'];
if($userRole == 1 ||  $userRole == 5 || $userRole == 8){
	//管理员,订单管理员,任务管理员+订单管理员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}


//更新订单内容
$doFlag = $_POST["doFlag"];
if($doFlag == "updateOrder"){
	$orderId = $_POST["orderId"];
	$_GET['id'] = $orderId;
	$order_no = $_POST["order_no"];
	$order_status = $_POST["order_status"];
	$battery_flag = $_POST["battery_flag"];
	$tracking_flag = $_POST["tracking_flag"];
	$payment_flag = $_POST["payment_flag"];
	$customer_name = $_POST["customer_name"];
	$customer_telephone = $_POST["customer_telephone"];
	$customer_address = $_POST["customer_address"];
	$customer_postcode = $_POST["customer_postcode"];
	$customer_email = $_POST["customer_email"];
	$deliver_flag = $_POST["deliver_flag"];
	$product_description = $_POST["product_description"];
	$customer_note = $_POST["customer_note"];
	$tracking_number = $_POST["tracking_number"];
	$express_company = $_POST["express_company"];
	$tracking_number_2 = $_POST["tracking_number_2"];
	$express_company_2 = $_POST["express_company_2"];
	
	$sql = "update orders set order_no = '".$order_no."'";
	$sql = $sql.", order_status='".$order_status."'";
	$sql = $sql.", battery_flag=".$battery_flag;	
	$sql = $sql.", tracking_flag=".$tracking_flag;
	$sql = $sql.", payment_flag=".$payment_flag;
	$sql = $sql.", customer_name='".$customer_name."'";
	$sql = $sql.", customer_telephone='".$customer_telephone."'";
	$sql = $sql.", customer_address='".$customer_address."'";
	$sql = $sql.", customer_postcode='".$customer_postcode."'";
	$sql = $sql.", customer_email='".$customer_email."'";
	$sql = $sql.", deliver_flag=".$deliver_flag;
	$sql = $sql.", product_description='".$product_description."'";
	$sql = $sql.", customer_note='".$customer_note."'";
	$sql = $sql.", tracking_number='".$tracking_number."'";
	$sql = $sql.", express_company='".$express_company."'";
	$sql = $sql.", tracking_number_2='".$tracking_number_2."'";
	$sql = $sql.", express_company_2='".$express_company_2."'";
	$sql = $sql."  where id=".$orderId;	
	$db->Execute($sql);
}else if($doFlag == "insertOrder"){
	//保存 并返回到列表
	$order_no = $_POST["order_no"];
	$order_site = $_POST["order_site"];
	$products_info = $_POST["products_info"];
	$order_status = $_POST["order_status"];
	$battery_flag = $_POST["battery_flag"];
	$tracking_flag = $_POST["tracking_flag"];
	$payment_flag = $_POST["payment_flag"];
	$customer_name = $_POST["customer_name"];
	$customer_telephone = $_POST["customer_telephone"];
	$customer_address = $_POST["customer_address"];
	$customer_postcode = $_POST["customer_postcode"];
	$customer_email = $_POST["customer_email"];
	$product_description = $_POST["product_description"];
	
	$sql_insert_orders = "INSERT INTO orders
		(create_time,order_no,order_site,products_info,order_status,battery_flag,tracking_flag,payment_flag,customer_name,customer_telephone,customer_address,customer_postcode,customer_email,product_description) 
		values 
		(NOW(),'$order_no','$order_site','$products_info',$order_status,$battery_flag,$tracking_flag,$payment_flag,'$customer_name','$customer_telephone','$customer_address','$customer_postcode','$customer_email','$product_description')";
		

	$db->Execute($sql_insert_orders);	
}


//订单详情 
$id = $_GET['id'];
if($id == ""){
	//新增订单
}else{
	//编辑订单
	$sql = "SELECT * from orders where id=".$id;
	$result = $db->Execute($sql);
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "订单详情"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>


<form name="inputForm" action="../order/orderInput.php" method="post">
<div id="container" >	
<div class="grayTable">	
<h4>订单详情:
<?php  if($doFlag == "updateOrder"){echo "订单内容更新成功!";}?>
</h4>
<table >
<thead>
	<tr>
		<th scope="col" width=90 >字段名称</th>
	    <th scope="col" >字段值</th>		
	</tr>
</thead>

<?php
if($id == ""){
	$strTR = "";
	$strTR = $strTR."<tr ><td>订单编号</td>";
	$strTR = $strTR."<td><input type='text' name='order_no' id='order_no'></td></tr>";
	$strTR = $strTR."<tr ><td>网站名称</td>";
	$strTR = $strTR."<td><input type='text' name='order_site' id='order_site' value='nosite'></td></tr>";
	$strTR = $strTR."<tr ><td>订单内容</td>";
	$strTR = $strTR."<td><textarea  cols='50'  rows='3' name='products_info' id='products_info' ></textarea></td></tr>";
	$strTR = $strTR."<tr ><td>订单状态</td>";
	$strTR = $strTR."<td><input type='text' name='order_status' id='order_status' value='20'><br>(已付款)</td></tr>";
	$strTR = $strTR."<tr ><td>电池状态</td>";
	$strTR = $strTR."<td><input type='text' name='battery_flag' id='battery_flag' value='0'><br>(0=没电池 1=有电池)</td></tr>";
	$strTR = $strTR."<tr ><td>跟单状态</td>";
	$strTR = $strTR."<td><input type='text' name='tracking_flag' id='tracking_flag' value='0'><br>(0=没跟单 1=有跟单)</td></tr>";
	$strTR = $strTR."<tr ><td>付款方式</td>";
	$strTR = $strTR."<td><input type='text' name='payment_flag' id='payment_flag' value='0'><br>(0=PayPal 1=Bank)</td></tr>";
	$strTR = $strTR."<tr ><td>客户名字</td>";
	$strTR = $strTR."<td><input type='text' name='customer_name' id='customer_name' size=32></td></tr>";
	$strTR = $strTR."<tr ><td>客户电话</td>";
	$strTR = $strTR."<td><input type='text' name='customer_telephone' id='customer_telephone' ></td></tr>";
	$strTR = $strTR."<tr ><td>客户地址</td>";
	$strTR = $strTR."<td><textarea  cols='50'  rows='3' name='customer_address' id='customer_address' ></textarea></td></tr>";
	$strTR = $strTR."<tr ><td>客户邮编</td>";
	$strTR = $strTR."<td><input type='text' name='customer_postcode' id='customer_postcode' ></td></tr>";
	$strTR = $strTR."<tr ><td>客户Email</td>";
	$strTR = $strTR."<td><input type='text' name='customer_email' id='customer_email' size=32></td></tr>";
	$strTR = $strTR."<tr ><td>产品描述</td>";
	$strTR = $strTR."<td><input type='text' name='product_description' id='product_description' size=32></td></tr>";
	$strTR = $strTR."<tr><td colspan=2 align=center>";
	$strTR = $strTR."<input type='button' value='保存' onclick='insertOrder()'>";
	$strTR = $strTR."<input type='button' value='返回' onClick='toList()'>";
	$strTR = $strTR."</td></tr>";
	echo $strTR;
}else{	
	if(!$result->EOF){
		$strTR = "";
		$strTR = $strTR."<tr ><td>ID</td>";
		$strTR = $strTR."<td>".$result->fields['id']."</td></tr>";
		$strTR = $strTR."<tr ><td>创建时间</td>";
		$strTR = $strTR."<td>".$result->fields['create_time']."</td></tr>";
		$strTR = $strTR."<tr ><td>订单编号</td>";
		$strTR = $strTR."<td><input type='text' name='order_no' id='order_no' value='".$result->fields['order_no']."' ></td></tr>";
		$strTR = $strTR."<tr ><td>Site</td>";
		$strTR = $strTR."<td>".$result->fields['order_site']."</td></tr>";
		$strTR = $strTR."<tr ><td>订单内容</td>";
		$strTR = $strTR."<td>".$result->fields['products_info']."</td></tr>";
		$strTR = $strTR."<tr ><td>订单状态</td>";
		$strTR = $strTR."<td><input type='text' name='order_status' id='order_status' value='".$result->fields['order_status']."' ><br>(10=没付款 20=已付款 30=已发货 40=已完成 50=已取消)</td></tr>";
		$strTR = $strTR."<tr ><td>是否带电池	</td>";
		$strTR = $strTR."<td ><input type='text' name='battery_flag' id='battery_flag' value='".$result->fields['battery_flag']."'><br>(是否有电池 0=没电池 1=有电池)</td></tr>";
		$strTR = $strTR."<tr ><td>是否跟单	</td>";
		$strTR = $strTR."<td><input type='text' name='tracking_flag' id='tracking_flag' value='".$result->fields['tracking_flag']."'><br>(是否转跟单 0=没跟单 1=有跟单)</td></tr>";
		$strTR = $strTR."<tr ><td>付款方式</td>";
		$strTR = $strTR."<td><input type='text' name='payment_flag' id='payment_flag' value='".$result->fields['payment_flag']."'><br>(付款方式 0=PayPal 1=Bank)</td></tr>";
		$strTR = $strTR."<tr ><td>付款时间</td>";
		$strTR = $strTR."<td>".$result->fields['pay_time']."</td></tr>";
		$strTR = $strTR."<tr ><td>客户名字</td>";
		$strTR = $strTR."<td><input type='text' name='customer_name' id='customer_name' value='".$result->fields['customer_name']."'></td></tr>";
		$strTR = $strTR."<tr ><td>客户电话</td>";
		$strTR = $strTR."<td><input type='text' name='customer_telephone' id='customer_telephone' value='".$result->fields['customer_telephone']."'></td></tr>";
		$strTR = $strTR."<tr ><td>客户地址</td>";
		$strTR = $strTR."<td><input type='text' name='customer_address' id='customer_address' value='".$result->fields['customer_address']."' size=64></td></tr>";
		$strTR = $strTR."<tr ><td>客户邮编</td>";
		$strTR = $strTR."<td><input type='text' name='customer_postcode' id='customer_postcode' value='".$result->fields['customer_postcode']."'></td></tr>";
		$strTR = $strTR."<tr ><td>客户Email</td>";
		$strTR = $strTR."<td><input type='text' name='customer_email' id='customer_email' value='".$result->fields['customer_email']."'></td></tr>";
		$strTR = $strTR."<tr ><td>快递状态</td>";
		$strTR = $strTR."<td><input type='text' name='deliver_flag' id='deliver_flag' value='".$result->fields['deliver_flag']."'><br>
		0=无需快递+缺货时标记为0; <br>
		1=走飞翔,还没单号;<br>
		10=走飞翔,已有单号;<br>
		2=走飞翔,单号已发客户;<br>
		4=走e邮宝,还没单号+货到标记为4;<br>
		5=走e邮宝,已有单号;<br>
		6=走e邮宝,单号已发客户;<br>
		7=走易运,还没单号;<br>
		8=走易运,已有单号;<br>
		9=走易运,单号已发客户;<br>
		11=走阿运,还没单号;<br>
		12=走阿运,已有单号;<br>
		13=走阿运,单号已发客户;<br>
		3=取消快递;<br>
		</td></tr>";
		$strTR = $strTR."<tr ><td>发货时间</td>";
		$strTR = $strTR."<td>".$result->fields['deliver_time']."</td></tr>";
		$strTR = $strTR."<tr ><td>填单时间</td>";
		$strTR = $strTR."<td>".$result->fields['trackingnumber_time']."</td></tr>";
		$strTR = $strTR."<tr ><td>产品描述</td>";
		$strTR = $strTR."<td><input type='text' name='product_description' id='product_description' value='".$result->fields['product_description']."' size=64><br>(给快递看的产品，数量，价格)</td></tr>";
		$strTR = $strTR."<tr ><td>客户备注</td>";
		$strTR = $strTR."<td><input type='text' name='customer_note' id='customer_note' value='".$result->fields['customer_note']."' size=64><br>(客户备注 送货时间要求,快递看)</td></tr>";
		$strTR = $strTR."<tr ><td>快递单号</td>";
		$strTR = $strTR."<td><input type='text' name='tracking_number' id='tracking_number' value='".$result->fields['tracking_number']."'></td></tr>";
		$strTR = $strTR."<tr ><td>快递公司</td>";
		$strTR = $strTR."<td><input type='text' name='express_company' id='express_company' value='".$result->fields['express_company']."'><br>(详见<设置>)</td></tr>";
		$strTR = $strTR."<tr ><td>快递单号(临时)</td>";
		$strTR = $strTR."<td><input type='text' name='tracking_number_2' id='tracking_number_2' value='".$result->fields['tracking_number_2']."'></td></tr>";
		$strTR = $strTR."<tr ><td>快递公司(临时)</td>";
		$strTR = $strTR."<td><input type='text' name='express_company_2' id='express_company_2' value='".$result->fields['express_company_2']."'><br>(详见<设置>)</td></tr>";
		$strTR = $strTR."<tr ><td>修改时间</td>";
		$strTR = $strTR."<td>".$result->fields['modify_time']."</td></tr>";
		$strTR = $strTR."<tr><td colspan=2 align=center>";
		$strTR = $strTR."<input type='button' value='保存' onclick='updateOrder(".$id.")'>";
		$strTR = $strTR."<input type='button' value='返回' onClick='toList()'>";
		$strTR = $strTR."</td></tr>";
		echo $strTR;
	}
}
?>

</table>



</div>

<br><br>
<br><br>

</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" >
<input type="hidden" name="orderId" id="orderId" >
</form>
</body>




</html>

<script>
<?php
if($doFlag == "insertOrder"){
	echo "window.location.href = 'orderList.php'"; 
}
?>

function updateOrder(id){
	document.getElementById('doFlag').value = 'updateOrder';
	document.getElementById('orderId').value = id;
	document.inputForm.submit();
}

function insertOrder(){
	document.getElementById('doFlag').value = 'insertOrder';	
	document.inputForm.submit();
}

function toList(){
	window.location.href = "orderList.php";
}
</script>

